import axios from 'axios'
import qs from 'qs'
import { ElMessage } from 'element-plus'
// import router from '@/router/index'

axios.defaults.timeout = 60000
axios.defaults.baseURL = process.env.NODE_ENV === 'development' ?
`${document.location.origin}` : 'https://xz.tengcity.com/city'
axios.defaults.withCredentials = true //携带cookie



/**
 * 响应拦截器
 * code 4    账号的时效期 过期 
 * code 103  无权限访问
 */
axios.interceptors.response.use(
	(response) => {
		const res = response.data;
		if (res.code !== 200) {
			ElMessage.closeAll()
			ElMessage.error(res.message);
			// return new Promise(() => {});
			return res
		} else {
            res.success = true
			return res;
		}
	},
	(error) => {
		ElMessage.closeAll();
		switch (error.response.status) {
			case 504:
			case 404:
				ElMessage.error('服务器被吃了( ╯□╰ )');
				break;
			case 403:
				ElMessage.error({
					message: '权限不足，请联系管理员'
				});
				
				break;
			default:
				ElMessage.error('error');
				break;
		}
		return Promise.reject(error);
	}
);



/**
 * get 请求方法
 * @param url
 * @param params
 * @returns {Promise}
 */
export function get(url, params = {}, headers = {}) {
    return new Promise((resolve, reject) => {
        axios.get(url, {
            params: params,
            headers: headers
        })
		.then(response => {
			resolve(response);
		})
		.catch(err => {
			reject(err)
		})
    })
}

/**
 * post 请求方法
 * @param url
 * @param data
 * @returns {Promise}
 */
export function post(url, data = {}) {
    return new Promise((resolve, reject) => {
        axios.post(url, data)
            .then(response => {
                resolve(response);
            }, err => {
                reject(err);
            })
    })
}

/**
 * postKeyValueRequest 请求方法
 * @param url
 * @param data
 * @returns {Promise}
 */
export function postKeyValueRequest(url, data = {}) {
    return new Promise((resolve, reject) => {
        axios.post(url, qs.stringify(data), {
			headers: {
				'Content-Type': 'application/x-www-form-urlencoded'
			}
		})
            .then(response => {
                resolve(response);
            }, err => {
                reject(err);
            })
    })
}



/**
 * delete 请求方法
 * @param url
 * @param data
 * @returns {Promise}
 */
export function del(url, data = {}) {
    return new Promise((resolve, reject) => {
        axios.delete(url, {data:qs.stringify(data)})
        .then(response => {
            resolve(response);
        }, err => {
            reject(err);
        })
    })
}

/**
 * patch 方法封装
 * @param url
 * @param data
 * @returns {Promise}
 */
export function patch(url, data = {}) {
    return new Promise((resolve, reject) => {
        axios.patch(url, data)
            .then(response => {
                resolve(response);
            }, err => {
                reject(err);
            })
    })
}

/**
 * put 方法封装
 * @param url
 * @param data
 * @returns {Promise}
 */
export function put(url, data = {}) {
    return new Promise((resolve, reject) => {
        axios.put(url, data)
            .then(response => {
                resolve(response);
            }, err => {
                reject(err);
            })
    })
}